<?php
require_once 'common.inc.php';

foreach($_POST as $_key=>$_value){
	!ereg("^\_",$_key) && $$_key=$_POST[$_key];
}
foreach($_GET as $_key=>$_value){
	!ereg("^\_",$_key) && $$_key=$_GET[$_key];
}

if('list'==$action)
{
	$limit=10;
	$page=intval($page)<1?1:intval($page);
	$start=(intval($page)-1)*$limit;
	$sql="select count(*) from ps_friendlink";
	$query=$dp->query($sql);
	$count=$dp->fetch_array($query);
	//print $count[0];
	$allpage=ceil($count['count(*)']/$limit);
	$page=$page>$allpage?$allpage:$page;
	$url='friendlink.php?action='.$action.'&amp;';
	$pages=numofpage($count['count(*)'],$page,$allpage,$url);
	
	$sql="select * from ps_friendlink limit $start,$limit";
	$query=$dp->query($sql);
	while($resulte=$dp->fetch_array($query)){
		$friendlinklist[]=$resulte;
	}

	$filename="../push/log/switchtxt.php";
		$str = file_get_contents($filename);	
		$str = explode(',',$str);

	include_once MCC_ROOT.'/templates/msystem/friendlinklist.htm';
}elseif('switch'==$action){

	$str = array_slice($_POST,0,-1);
	$str = implode(',',$str);

	$filename="../push/log/switchtxt.php";
	$fp=fopen($filename,'wb+');
	if($fp){
		fwrite($fp,$str);
		fclose($fp);
		
		$sql="select * from ps_friendlink where ascription='0'";
		$query=$dp->query($sql);
		while($result=$dp->fetch_array($query)){
			$dnet[]=$result;
		}

		$sql="select * from ps_friendlink where ascription='1'";
		$query=$dp->query($sql);
		while($result=$dp->fetch_array($query)){
			$vnet[]=$result;
		}
		$aaa=creathtml($dnet);
		$filename="../push/log/show1.php";
		$fp=fopen($filename,'wb+');
		if($fp){
			file_put_contents($filename,$aaa);
		}else{
			adminmsg('更新缓存失败',"friendlink.php?action=list");
		}

		$bbb=creathtml($vnet);
		$filename="../push/log/show2.php";
		$fp=fopen($filename,'wb+');
		if($fp){
			file_put_contents($filename,$bbb);
		}else{
			adminmsg('更新缓存失败',"friendlink.php?action=list");
		}

		adminmsg('更新内容操作成功',"friendlink.php?action=list");
	}else{
		adminmsg('更新内容操作失败',"friendlink.php?action=list");
	}


}elseif('edit'==$action){
	$action='oedit';
	$sql="select * from ps_friendlink where id='$id'";
	$query=$dp->query($sql);
	while($resulte=$dp->fetch_array($query)){
		$friendlinklist=$resulte;
	}
	include_once MCC_ROOT.'/templates/msystem/friendlinkadd.htm';
}elseif('oedit'==$action){

	$sql="update ps_friendlink set url='$url',picurl='$picurl' where id='$id'";
	$query=$dp->query($sql);

	adminmsg('修改成功','friendlink.php?action=list');

}elseif('delete'==$action){
	$sql="delete from ps_friendlink where id='$id'";
	$query=$dp->query($sql);
	adminmsg('删除成功','friendlink.php?action=list');

}elseif('add'==$action){
	$action='oadd';
	include_once MCC_ROOT.'/templates/msystem/friendlinkadd.htm';

}elseif('oadd'==$action){

	$sql="insert into ps_friendlink (url,picurl,ascription) values ('$url','$picurl','$ascription')";
	$query=$dp->query($sql);
	adminmsg('添加成功','friendlink.php?action=list');
}


function creathtml($str){
	$i=0;
	foreach($str as $list){
		$html[]="
	<td align=\"center\" valign=\"middle\"><a href=\"$list[url]\" target=\"_blank\"><img src=\"$list[picurl]\" width=\"150\" height=\"40\" border=\"0\" /></a></td>";
	$i++;
	if('0'==$i%4){$html[]="</tr><tr>";}
	}
	if('0'!=$i%4){
		$y=(4-$i%4);
		for($x=0;$x<$y;$x++){
			$html[]="
	<td align=\"center\" valign=\"middle\">&nbsp;</td>";
		}
	}
	return $html;
}
?>